

package MaintMenu.VenPricingUpdTBLN;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;

public class DEPOtbl {

    public DEPOtbl(int z)    {  //0 = partlink ; 1 = part#
        int cc=0;
        String dbInternal="";
        String dbExternal="";
        int capa=0;

        if  (z==0){
            dbInternal="partlink";
//            dbExternal="PartsLink1";   //old when partslink wasn't created
            dbExternal="PartsLink";

        }if (z==1) {
            dbInternal="[part#]";
            dbExternal="prtlnk";
        }



        //create statement object
        SQLUpdStm stm = new SQLUpdStm();

        String tableA, tableB,onA,onB, whereA,whereB,whereoperator,
                fieldA1,fieldB1,
                fieldA2,fieldB2,
                fieldA3,fieldB3,
                fieldA4,fieldB4,
                fieldA5,fieldB5,
                fieldA6,fieldB6,
                fieldA7,fieldB7,
                fieldA8,fieldB8,
                fieldA9,fieldB9,
                fieldA10,field10;
        int filesToUpdate=4;
        do {
            //A = LOCAL
            //B = Ext. dat
            whereA="";
            whereB="";
            whereoperator="";
            if (capa==0){
                //for non capa
                whereA="WHERE A.CAPA<>YES";        ///****************** CAPA=NO (USE THIS FOR ACCESS BOOLEAN FIELDS TO IDENTIFY TRUE OR FALSE
//                        whereA="WHERE ";
                whereB="B.CAPA<>YES";
                whereoperator="AND";
//                        whereoperator="";
                filesToUpdate=1;

            }else if (capa==1){


                //for capa items
                whereA="WHERE A.CAPA=YES";      ///****************** CAPA=YES (USE THIS FOR ACCESS BOOLEAN FIELDS TO IDENTIFY TRUE OR FALSE
                whereB="B.CAPA=YES";
                whereoperator="AND";
                //reset loop for filesToUpdate
                filesToUpdate=1;

            }

            while (filesToUpdate<=4){    //4



                try {

                    Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
                    String dataSourceName = "dbLI";
                    String dbURL = (new StringBuilder()).append("jdbc:odbc:").append(dataSourceName).toString();
                    Connection conli = DriverManager.getConnection(dbURL, "", "");


                    String st[] = new String[3];





                     /* update mirror master file */   // NON CAPA
    //                whereA="WHERE A.CAPA=YES";
    //                whereB="B.CAPA=TRUE";
    //                whereoperator="AND";
    //                whereA="where a.DEPO_P";
    //                whereB="b.[unit price]";
    //                whereoperator="<>";
//                    whereA="WHERE A.CAPA IS NULL";
//                    whereB="B.CAPA=FALSE";
//                    whereoperator="AND";

    //                whereA="WHERE A.CAPA=YES";
    //                whereB="B.CAPA=TRUE";
    //                whereoperator="AND";

//                    whereA="";
//                    whereB="";
//                    whereoperator="";


                    tableA =    "tblmrn";
                    tableB =    "_costdepo"+filesToUpdate;
                    onA=        dbInternal;
                    onB=        dbExternal;

//                    fieldA1=    "oem_no";       //part_no  (oem#)
//                    fieldB1=    "[OEM 1]";

                    fieldA2=    "[depo#]";       //DEPOT #
                    fieldB2=    "[DEPO NO]";

                    fieldA3=    "DEPO_P";      //PRICE
                    fieldB3=    "[UNIT PRICE]";

                    fieldA4=    "DEPO_k";            //Package
                    fieldB4=    "[Qty Ctn]";
                    st[0] = stm.StmExecute(tableA,tableB,whereA,whereB,whereoperator,onA,onB,fieldA2,fieldB2,fieldA3,fieldB3,fieldA4,fieldB4);
        //            System.out.println(st[0]);

                                 /* update Cooling master file */
    //                whereA="where a.DEPO_P";
    //                whereB="b.[unit price]";
    //                whereoperator="<>";
                    tableA =    "tblcln";
                    tableB =    "_costdepo"+filesToUpdate;
                    onA=        dbInternal;
                    onB=        dbExternal;
//
//                    fieldA1=    "oem_no";       //part_no  (oem#)
//                    fieldB1=    "[OEM 1]";

                    fieldA2=    "[depo#]";       //DEPOT #
                    fieldB2=    "[DEPO NO]";

                    fieldA3=    "DEPO_P";      //PRICE
                    fieldB3=    "[UNIT PRICE]";

                    fieldA4=    "DEPO_k";            //Package
                    fieldB4=    "[Qty Ctn]";
                    st[1] = stm.StmExecute(tableA,tableB,whereA,whereB,whereoperator,onA,onB,fieldA2,fieldB2,fieldA3,fieldB3,fieldA4,fieldB4);
        //            System.out.println(st[1]);

                     /* update LT_MAST11 master file */
    //                whereA="where a.DEPO_P";
    //                whereB="b.[unit price]";
    //                whereoperator="<>";
                    tableA =    "tblltn";
                    tableB =    "_costdepo"+filesToUpdate;
                    onA=        dbInternal;
                    onB=        dbExternal;
//
//                    fieldA1=    "oem_no";       //part_no  (oem#)
//                    fieldB1=    "[OEM 1]";

                    fieldA2=    "[depo#]";       //DEPOT #
                    fieldB2=    "[DEPO NO]";

                    fieldA3=    "DEPO_P";      //PRICE
                    fieldB3=    "[UNIT PRICE]";

                    fieldA4=    "DEPO_k";            //Package
                    fieldB4=    "[Qty Ctn]";
                    st[2] = stm.StmExecute(tableA,tableB,whereA,whereB,whereoperator,onA,onB,fieldA2,fieldB2,fieldA3,fieldB3,fieldA4,fieldB4);
        //            System.out.println(st[2]);

                    for (int x=0; x<st.length;x++){
                        Statement stli = conli.createStatement();

                        try {
//                            System.out.println(st[x]);
                            int rowsupdated = stli.executeUpdate(st[x]);
//                            System.out.print(" TRU : capa"+capa+" "+tableB+" :" + rowsupdated + "\t");
                            //JOptionPane.showOptionDialog(null, "Statement : " + st[x] + "\n\nTotal record Updated : " + rowsupdated, "Update Complete", JOptionPane.DEFAULT_OPTION, JOptionPane.INFORMATION_MESSAGE, null, new Object[]{}, null);
                            cc++;
                            if (cc==2){
//                                System.out.println("");
                                cc=0;
                            }

                        }catch(SQLException er) {
                            er.printStackTrace();
                        }stli.close();
                    }     conli.close();
                    filesToUpdate++;
                }

                catch(Exception e){
                    e.printStackTrace();
                }





            }

//            System.out.println(capa);
//            System.out.println(filesToUpdate);

            capa++;
        } while (capa<2);




    }
}